package com.hzky.demo.controller;

import com.hzky.demo.dao.*;
import com.hzky.demo.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by admin on 2017/6/6.
 */
@Transactional
@RestController
@RequestMapping(value = "/test")
public class DatebaseTest {

    @Autowired
    TeacherDao teacherDao;

    @Autowired
    StudentDao studentDao;

    @Autowired
    SchoolDao schoolDao;

    @Autowired
    ParentDao parentDao;

    @Autowired
    Teacher_studentDao teacher_studentDao;

    @Autowired
    CourseDao courseDao;

    @Autowired
    Parents_courseDao parents_courseDao;

    @Autowired
    HomeworkDao homeworkDao;

    @RequestMapping(value = "/dao/school")
    String insertSchool()
    {

        for(int i=0;i<10;i++)
        {
            School school=new School();
            school.setName("test"+String.valueOf(i));
            school.setLocation("testLoaction;"+String.valueOf(i));
            schoolDao.save(school);
        }

        return "ok";
    }

    @RequestMapping(value = "/dao/teacher")
    String inserttheteacher()
    {
        List<School> schools=schoolDao.findAll();
        for(int i=0;i<100;i++)
        {
            Teacher teacher=new Teacher();
            teacher.setPhone_number("test"+String.valueOf(i));
            School school=schools.get(i%schools.size());
            teacher.setSchool(school);
            teacher.setLocation(school.getLocation());
            teacher.setName("test"+String.valueOf(i));
            teacher.setPassword(String.valueOf(i)+"123456");
            teacherDao.save(teacher);

        }

        return "ok";
    }

    @RequestMapping(value = "/dao/parents")
    String insertParents()
    {

        List<School> schools=schoolDao.findAll();
        for(int i=0;i<1000;i++)
        {
            Parent parent=new Parent();
            parent.setPhone_number("test"+String.valueOf(i));
            parent.setName("test"+String.valueOf(i));
            parent.setPassword(String.valueOf(i)+"123456");
            parent.setSchool(schools.get(i%schools.size()));
            parent.setLocation(schools.get(i%schools.size()).getLocation());
            parentDao.save(parent);
        }

        return "ok";
    }



    @RequestMapping(value = "/dao/student")
    String insertstudent()
    {
        List<Parent> parents=parentDao.findAll();
        for(int i=0;i<1500;i++)
        {
            Student student=new Student();
            student.setName("test"+String.valueOf(i));
            student.setParent(parents.get(i%parents.size()));
            studentDao.save(student);
        }
        return "ok";
    }

    @RequestMapping(value = "/dao/course")
    String insertCourse()
    {
        List<Teacher> teachers=teacherDao.findAll();
        for(int i=0;i<10000;i++)
        {
            Course course=new Course();
            course.setName("test"+i);
            course.setTeacher(teachers.get(i%teachers.size()));
            course.setExtra("just for test");
            courseDao.save(course);
        }
        return "ok";
    }
    @RequestMapping(value = "/dao/parents_course")
    String insertParentsCourse()
    {
        List<Parent> parents=parentDao.findAll();
        for(int i=0;i<20;i++)
        {
            Parents_course parents_course=new Parents_course();
            parents_course.setParent(parents.get(i));
            parents_course.setCourse(courseDao.getOne(1));
            parents_courseDao.save(parents_course);
        }
        return "ok";
    }

    @RequestMapping(value = "/dao/teacher_student")
    String insertTeacherStudent()
    {
        Random random=new Random();

        List<Teacher> teachers=teacherDao.findAll();
        List<Student> students=studentDao.findAll();
        for(int i=0;i<3000;i++)
        {
            Teacher_student teacher_student=new Teacher_student();
            teacher_student.setStudent(students.get(random.nextInt(students.size())));
            teacher_student.setTeacher(teachers.get(random.nextInt(teachers.size())));
            teacher_studentDao.save(teacher_student);
        }
        return "ok";
    }

    @RequestMapping(value = "/dao/homework")
    String insertHomework()
    {
        Random random=new Random();

        List<Course> courses=courseDao.findAll();
        for(int i=0;i<2000;i++)
        {
            Homework homework=new Homework();
            homework.setDate(new Date());
            homework.setInformation("Just for test");
            homework.setCourse(courses.get(random.nextInt(200)));
            homework.setName("test"+String.valueOf(i));
            homeworkDao.save(homework);
        }
        return "ok";
    }
}
